//
//  SearchView.m
//  HuaRun
//
//  Created by admin on 17/8/28.
//  Copyright © 2017年 Sunny. All rights reserved.
//

#import "SearchView.h"

@interface SearchView () <UITextFieldDelegate>
//确定按钮
@property (nonatomic, strong) UIButton * searchBtn;
@property (nonatomic, strong) UIView * bgview;
@end

@implementation SearchView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
#pragma mark - life cycle
- (void)dealloc {
    NSLog(@"%@ dealloc", [self class]);
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self viewInit];
        [self layoutInit];
    }
    return self;
}

- (void)viewInit {
//    self.backgroundColor = kRandomColor;// [UIColor whiteColor];
    self.backgroundColor = RGBA(249, 249, 249, 1);

    [self addSubview:self.bgview];
    
    self.textField = [UITextField new];
//    _textField.placeholder = self.placeholderStr ? self.placeholderStr : @"";
    _textField.font = kGeneralFontSize;
    _textField.delegate = self;
    _textField.backgroundColor = [UIColor whiteColor];
    _textField.returnKeyType = UIReturnKeySearch;
    
    [self addSubview:_textField];
        
    [self addSubview:self.searchBtn];
    
}

- (void)layoutInit {
    weakself(ws);
    
    CGFloat height = 50;
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(ws);
        make.height.mas_equalTo(height);
    }];
    
    [self.bgview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(ws.textField).offset(-2);
        make.bottom.mas_equalTo(ws.textField).offset(2);
        make.left.mas_equalTo(ws.textField).offset(-kMargin10);
        make.right.mas_equalTo(ws.searchBtn).offset(kMargin10);
    }];
    
    [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(ws);
        make.left.mas_equalTo(ws).offset(kMargin10 * 3);
        make.right.mas_equalTo(ws.searchBtn.mas_left).offset(-kMargin10);
        make.height.mas_equalTo(30);
    }];
    
    [self.searchBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(ws);
        make.right.mas_equalTo(ws).offset(-kMargin10 * 3);
        make.size.mas_equalTo(CGSizeMake(20, 20));
    }];
}

- (UIButton *)searchBtn {
    if (!_searchBtn) {
        self.searchBtn = [UIButton new];
        [_searchBtn setBackgroundImage:[UIImage imageNamed:@"搜索"]
                              forState:(UIControlStateNormal)];
        [_searchBtn setBackgroundImage:[UIImage imageNamed:@"搜索"]
                              forState:(UIControlStateSelected)];
        [_searchBtn addTarget:self
                       action:@selector(searchMethod)
             forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _searchBtn;
}

#pragma mark -- UITextFieldDelegate
- (void)textFieldDidBeginEditing:(UITextField *)textField {
    NSLog(@"begin editing");
//    self.clickBlock ? self.clickBlock(@"") : nil;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    NSLog(@"点击了搜索");
    _clickBlock ? _clickBlock (@"") : nil;
    return YES;
}
    
- (void)searchMethod {
    self.clickBlock ? self.clickBlock(@"") : nil;

}

- (void)setPlaceholderStr:(NSString *)placeholderStr {
    _placeholderStr = placeholderStr;
    self.textField.placeholder = placeholderStr;
}

- (UIView *)bgview {
    if (!_bgview) {
        self.bgview = [UIView new];
        _bgview.backgroundColor = [UIColor whiteColor];
        _bgview.layer.masksToBounds = YES;
        _bgview.layer.cornerRadius = 17;
        _bgview.layer.borderWidth = 1;
        _bgview.layer.borderColor = RGBA(244, 244, 244, 1).CGColor;
    }
    return _bgview;
}
@end
